import logging
import allure
import pytest
import requests
from help_api_master.api_keyword.api_key import ApiKey
from help_api_master.params.allParams import URL

# 逻辑层
class cancel_order():
    token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwa19pZCI6MSwiYV9pZCI6IiIsImR0eXBlIjoxLCJwaG9uZSI6Im51bGwiLCJyb2xlX2lkIjpudWxsLCJ0eXBlIjoieXp5IiwiaWF0IjoxNzQ3NjE0MTU3LCJleHAiOjE3NDc2NTczNTd9.ZeGTCoDEuFZh7oa_hD7OMVrsZrKlP2O5NzQdszVJRuw'
    """登录"""
    def test_login(self):
        allure.dynamic.title("登录")
        apiKey = ApiKey()
        userInfo = {
            "js_code": "0e3AaY100QWUWT1X7u000XkK1k4AaY17"
        }
        url = f"{URL}/api/wx/user/login"
        response = apiKey.post(
            url=url,
            json=userInfo,
            headers={"Content-Type": "application/json"}
        )
        with allure.step(f"发送登录请求"):
            msg = apiKey.get_text(response.text, 'msg')
            print(f"响应消息：{msg}")
            assert msg == '登录成功'


    """获取服务列表"""
    def test_List(self):
        apikey = ApiKey()
        # apiKey, token, res, token_random, uid = token_fix  # 从fix中获取预置的工具类和token和uid
        allure.dynamic.title('获取服务列表')  # 动态获取参数生成标题
        with allure.step(f"获取服务列表"):

            # 准备请求数据
            json = {
                "uid": "14172",
            }
            url = f"{URL}/api/server/get/uid"  # 构建完整的 URL
            headers = {'token':self.token}
            response1 = apikey.post(url=url, headers=headers, json=json)
        with allure.step(f"接口返回信息校验接打印"):
            print(f'查看服务列表响应消息:{response1.text}')
            result = response1.json()  # 获取响应结果（只调用一次 json()）
            msg = result.get('msg', '未找到msg字段')  # 获取响应消息
            print(f"响应消息：{msg}")
            #断言响应消息
            # assert result.get('msg') == '登录态已失效，请重新登录', (
            #     f"响应消息与预期不符\n"
            #     f"预期消息：{'获取成功'}\n"
            #     f"实际消息：{result.get('msg')}"
            # )
        return response1


    """查看发布的服务"""
    def test_addhelp(self):
        apikey = ApiKey()
        allure.dynamic.title('发起发布服务')  # 动态获取参数生成标题
        # apiKey, token, res, token_random, uid = token_fix  # 从fix中获取预置的工具类和token和uid
        with allure.step('发送发布请求'):
            url = f"{URL}/api/help/add"  # 构建完整的 URL
            json = {
                "openid": "o6Kmm5Fy9OwYyt4E0EuFr0qQQ_Yg",
                "mu": "昌平",
                "wx_id": "1345",
                "form_id": "the formId is no longer available in develop or trial version of this mini program",
                "title": "快递代取",
                "des": "中件 111",
                "total_fee": "2"
            }
            headers = {'token': self.token}
            response2 = apikey.post(url=url, headers=headers, json=json)
        with allure.step(f"接口返回信息校验接打印"):
            result = response2.json()  # 获取响应结果（只调用一次 json()）
            print(f'查看发出的服务列表响应消息:{response2.text}')
            msg = result.get('msg', '未找到msg字段')  # 获取响应消息
            print(f"响应消息：{msg}")
            # 断言响应消息
            assert result.get('msg') == '操作成功', (
                f"响应消息与预期不符\n"
                f"预期消息：{'操作成功'}\n"
                f"实际消息：{result.get('msg')}"
            )
            # assert ApiKey.db_mysql_select(table='helplist',field='id',value=apikey.get_text(response2.text,'oid')) == 1,'未找到对应订单'
        return response2

    """确认取消"""
    def test_pay(self):
        apikey=ApiKey()
        allure.dynamic.title('确认取消')  # 动态获取参数生成标题
        # apiKey, token, res, token_random, uid = token_fix  # 从fix中获取预置的工具类和token和uid
        with allure.step('发送取消请求'):
            res1=self.test_addhelp()
            url = f"{URL}/api/help/update/state"  # 构建完整的 URL
            json = {
                "state": "4",
                "id": apikey.get_text(res1.text,'oid')
            }
            headers = {'token':self.token}
            response = apikey.post(url=url, headers=headers, json=json)
        with allure.step(f"接口返回信息校验接打印"):
            result = response.json()  # 获取响应结果（只调用一次 json()）
            msg = result.get('msg', '未找到msg字段')  # 获取响应消息
            print(f"响应消息：{msg}")
            # 断言响应消息
            assert result.get('msg') == '修改成功', (
                f"响应消息与预期不符\n"
                f"预期消息：{'操作成功'}\n"
                f"实际消息：{result.get('msg')}"
            )
        return response

